package com.hualian.coupon.controller;

import com.hualian.coupon.exception.BusinessException;
import com.hualian.coupon.pojo.Coupon;
import com.hualian.coupon.pojo.Member;
import com.hualian.coupon.service.MemberService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author Lxd
 * @Description TODO
 * @date 2020/10/23 13:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/mem")
public class MemberController {

    @Resource
    MemberService memberService;
    @Resource
    RedisTemplate<String ,Object> redisTemplate;
    @PostMapping("/reg")
    public String register(@RequestParam("name") String name, @RequestParam("phone") String phone,
                           @RequestParam("captcha") String captcha) {
        Member byPhone = memberService.findByPhone(phone);
        if (byPhone != null) {
            throw new BusinessException("该手机已注册");
        }
        //判断验证码是否合法
        BoundValueOperations<String, Object> operations = redisTemplate.boundValueOps("sms:" + phone);
        Object o = operations.get();
        System.out.println(o);
        if (captcha.equals(o)) {
            //比较成功,继续实现注册功能
            memberService.add(name, phone);
        } else {
            throw new BusinessException("验证失败");
        }

        return "注册成功";
    }

    @PostMapping("/login")
    public Member login(@RequestParam("phone") String phone) {

        Member member = memberService.findByPhone(phone);
        if (member == null) {
            throw new BusinessException("该手机号尚未注册");
        }
        return member;
    }

    @RequestMapping("/exMemberList")
    public void handle(HttpServletResponse response) throws IOException {
        //获取会员信息
        List<Member> members = memberService.queryAll();

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("会员信息");
        String fileName = "会员.xls";

        //实现中文文件名转换
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        //headers表示Excel表中第一行的表头
        String[] headers = {"id","name","phone","regDate","status"};
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //处理时间格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //添加数据行
        int rowNum = 1;
        for (Member item : members
        ) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getPhone());
            dataRow.createCell(3).setCellValue(dateFormat.format(item.getRegDate()));
            dataRow.createCell(4).setCellValue(item.getStatus());
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        //以附件形式访问
        response.setHeader("Content-disposition","attachment;filename="+fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    @PostMapping("/getCopList")
    public Coupon getCopList(@RequestParam String phone) {

        return null;
    }
}
